package com.orhon.smartcampus.modules.student.fetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.dao.EclassHeadteacherDao;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.student.entity.EclassHeadteacher;
import com.orhon.smartcampus.modules.student.gqlModel.GQLEclass;
import com.orhon.smartcampus.modules.student.gqlModel.GQLHeadTeacher;
import com.orhon.smartcampus.modules.student.mm.EclassHeadteacherMapper;
import com.orhon.smartcampus.modules.student.mm.EclassMapper;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.teacher.mm.TeacherMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;


@Component
public class EclassHeadteachersFetcher implements DataFetcher {

	@Autowired
	EclassHeadteacherDao eclassHeadteacherDao;

	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		Long schoolId = JwtUtils.getSchoolId(token);

		List<GQLHeadTeacher> GQLHeadTeacher = new ArrayList<>();


		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from EclassHeadteacher where deleted_at = null ";

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("id")!=null) {
				hql += " and id " + Util.decide(map.get("id"));
			}
			if(map.get("eclass_id")!=null) {
				hql += " and eclass_id " +  Util.decide(map.get("eclass_id"));
			}
			if(map.get("user_id")!=null) {
				hql += " and user_id " + Util.decide(map.get("user_id"));
			}

			if(map.get("teacher_id")!=null) {
				hql += " and teacher_id  " +Util.decide(map.get("teacher_id"));
			}
			if(map.get("school_id")!=null) {
				hql += " and school_id " + Util.decide(map.get("school_id"));
			}
			if(map.get("status")!=null) {
				hql += " and status" + Util.decide(map.get("status"));
			}

		}
System.out.println(hql);
		List<EclassHeadteacher> list = eclassHeadteacherDao.find(hql,null,page.getPage(),page.getLimit());

		for (EclassHeadteacher item : list) {
			GQLEclass gqlEclass = null;
			if (env.getSelectionSet().contains("data/eclass")){
				Eclass eclass = item.getStudentEclassByEclassId();
				gqlEclass = EclassMapper.MAPPER.toEclass(eclass);
			}

			GQLTeacher gqlTeacher = null;
			if (env.getSelectionSet().contains("data/teacher")){
				TInformation tInformation = item.getTeacherTInformationByTeacherId();
				gqlTeacher = TeacherMapper.MAPPER.toTeacher(tInformation);
			}

			GQLSchool gqlSchool = null;
			if (env.getSelectionSet().contains("data/school")){
				Schools schools = item.getBaseSchoolsBySchoolId();
				gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
			}

			GQLHeadTeacher teacher = EclassHeadteacherMapper.MAPPER.mapEntity(item);
			teacher.setEclass(gqlEclass);
			teacher.setSchool(gqlSchool);
			teacher.setTeacher(gqlTeacher);
			GQLHeadTeacher.add(teacher);

		}

		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = eclassHeadteacherDao.getTotalCount(hql);
		}
		return Result.ok().put("data", GQLHeadTeacher).put("total", total);
	}


}
